<template>
  <div class="cus-student-form" ref="formRef">
    <el-form :model="formData" label-width="90px" :rules="rules">
      <el-form-item label="学校" prop="schoolName">
        <!-- <el-input v-model="formData.schoolName" class="cus-input" placeholder="请输入"></el-input> -->
        <el-autocomplete
          v-model="formData.schoolName"
          :fetch-suggestions="querySearchAsync"
          placeholder="请输入"
          style="width:348px"
        ></el-autocomplete>
      </el-form-item>
      <el-form-item label="平时成绩" prop="achievement">
        <el-select v-model="formData.achievement" class="cus-input" placeholder="请选择成绩">
          <el-option label="前2%" value="前2%"></el-option>
          <el-option label="前5%" value="前5%"></el-option>
          <el-option label="前10%" value="前10%"></el-option>
          <el-option label="前20%" value="前20%"></el-option>
          <el-option label="前50%" value="前50%"></el-option>
          <el-option label="后20%" value="后20%"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="性格" prop="nature">
        <el-input v-model="formData.nature" class="cus-input" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="优势学科" prop="majorSubjects">
        <el-input v-model="formData.majorSubjects" class="cus-input" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="弱势学科" prop="weakSubjects">
        <el-input v-model="formData.weakSubjects" class="cus-input" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="入学测成绩" prop="testAchievement">
        <div class="cus-item">
          <el-input
            v-model.number="formData.haveScore"
            onkeyup="value=value.replace(/[^\d]/g,'')"
            placeholder="请输入得分"
          ></el-input>
          <span>/</span>
          <el-input
            v-model.number="formData.totalScore"
            onkeyup="value=value.replace(/[^\d]/g,'')"
            placeholder="请输入总分"
          ></el-input>
        </div>
      </el-form-item>
      <!-- <el-form-item label="整体评价" prop="comment">
        <el-input
          v-model="formData.comment"
          class="cus-input"
          type="textarea"
          rows="3"
          placeholder="请输入"
        ></el-input>
      </el-form-item>-->
    </el-form>
  </div>
</template>

<script lang="ts">
import { ref } from 'vue'
import axios from 'axios'
export default {
  props: {
    studentData: {
      type: Object,
      default: {},
    },
  },
  setup(props) {
    let formData = ref({
      schoolName: '',
      achievement: '',
      nature: '',
      majorSubjects: '',
      weakSubjects: '',
      totalScore: '',
      haveScore: '',
      comment: '',
    })
    const validateScore = (rule, value, callback) => {
      if (parseFloat(formData.value.totalScore) === 0) {
        callback(new Error('总分必须大于“0”'))
      } else if (
        parseFloat(formData.value.totalScore) <
        parseFloat(formData.value.haveScore)
      ) {
        callback(new Error('总分不得低于得分'))
      } else {
        callback()
      }
    }
    let rules = {
      testAchievement: [{ validator: validateScore, trigger: 'blur' }],
    }
    for (let key in props.studentData) {
      formData.value[key] = props.studentData[key]
    }
    if (props.studentData.entranceExam) {
      formData.value.haveScore = props.studentData.entranceExam.split('/')[0]
      formData.value.totalScore = props.studentData.entranceExam.split('/')[1]
    }
    const querySearchAsync = async (val, cb) => {
      axios
        .post('/admin/publicSchool/queryPage', {
          name: val,
          current: 1,
          size: 100,
        })
        .then((res: any) => {
          let arr = res.records.map((e) => {
            e.value = e.name
            return e
          })
          cb(arr)
        })
    }
    const save = (resolve, reject) => {
      resolve(formData.value)
    }
    return { formData, querySearchAsync, save, validateScore, rules }
  },
}
</script>

<style scoped lang="scss">
.cus-student-form {
  .cus-input {
    width: 348px;
  }
  .cus-item {
    > span {
      margin: 0 12px;
    }
    :deep(.el-input) {
      width: 113px;
    }
  }
}
</style>
